package com.base.util;

import android.content.Context;
import android.graphics.Bitmap;

import com.base.control.MActivitySupport;
import com.base.model.Bean2Json;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.io.File;
import java.util.UUID;

public class ConstantResource {
    public static String PROJECT_NAME = "YJH"; //用于创建包含该名字的文件目录，
    public static String APK_NAME = "APK_NAME";

    public static FileHelper fileHelper = new FileHelper();
    public static ImageLoader imageLoader = ImageLoader.getInstance(); //单例imageLoader
    public static String APPID = "";
    public static String MD5_KEY = "";
	public static int BUFF_SIZE = 2048;	//文件读写缓存区大小

    //intent key
    public static String LOGIN_VERIFY_ERROR_KEY = "IS_LOGIN_VERIFY_ERROR";
    public static String START_TYPE = "START_TYPE";
    public static String START_DATA = "START_DATA";

    public static Class<?> DATA_CLASS[] = {Bean2Json.class};
    public static Class<? extends MActivitySupport> LOGIN_ACTIVITY_CLASS;

	private static String[] WEEK_NAMES = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};

	public static boolean IS_SHOW_LOG = true;

    //请求地址
	public static String URL = "http://114.215.196.179:8080/gs/mobile";
    public static String DOWNLOAD_URL = URL + "/download.do";
    public static String UPLOAD_URL = URL + "/fileUpload.do";

    public static Integer CACHE_DAY = 50;
    public static String TEMP_PATH = "YJH_FRAME";

	public static String USERID = "2";
	public static String VERIFY = "1";
	
	//Error for Toast
	public static String NET_ERROR = "无网络，请重新连接";
	public static String INTERFACE_ERROR = "接口处理错误";

    public static DeviceUuidFactory deviceUuidFactory;

    public static void setImageLoader(Context context) {
        DisplayImageOptions options = new DisplayImageOptions.Builder() //craet options
                .cacheInMemory(false)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .defaultDisplayImageOptions(options)
                        //.threadPriority(Thread.NORM_PRIORITY - 2)
                        //.memoryCache(new LruMemoryCache(maxSize))
                .memoryCache(new WeakMemoryCache())
                .denyCacheImageMultipleSizesInMemory()
                .build();
        ConstantResource.imageLoader.init(config);
    }


	/**
	 * 返回所需周中文名
	 * @param i
	 * @return
	 */
	public static String getWeekName(int i) {
		String result = "其他";
		
		if(i >= 0 && i < WEEK_NAMES.length) {
			result = WEEK_NAMES[i];
		}
		
		return result;
	}
	
	/**
	 * 返回deviceid
	 * @return
	 */
	public static String getDeviceid() {
		return StringUtil.isEmpty(deviceUuidFactory.getDeviceUuid().toString()) ? UUID.randomUUID().toString() : deviceUuidFactory.getDeviceUuid().toString();
    }

    public static boolean hasImgCache(String imgId, int w, int h) {
        boolean flag = true;
        File f = ConstantResource.imageLoader.getDiskCache().get(UrlUtils.getFileUrl(imgId, w, h));
        if(f == null || !f.exists()) { //有缓存就用loading了
            flag = false;
        }
        return flag;
    }
}
